import java.io.File;

/**
 * Created by lrs on 2017/12/30.
 */
public class ErgFile {
    Handle handle = new Handle();

    /*
     * 通过递归得到某一路径下所有的目录及其文件
     */
    public void getFiles(String filePath, String source, String target) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("处理目录" + file.getAbsolutePath());
                this.handle.directory(file, source, target);
                getFiles(file.getAbsolutePath(), source, target); // 递归调用
            } else {
                boolean needType = false;

                String sourceFileName = file.getAbsolutePath().replace("\\", "/");
                String fileName = file.getName();
                for(String fileType : WebResRepair.needType) {
                    int i = sourceFileName.indexOf(fileType);
                    if ((i!=-1) && (i+fileType.length()==sourceFileName.length())) {
                        String targetFile = target + sourceFileName.replace(source,"");

                        System.out.println("替换指定文件内容：" + sourceFileName + " 至 " + targetFile);

                        this.handle.handleFile(fileName, sourceFileName, targetFile, null); // 使用文本查询的方式处理

                        needType = true;
                        break;
                    }
                }

                if (!needType) {
                    String targetFile = target + sourceFileName.replace(source,"");
                    System.out.println("复制文件：" + sourceFileName + " 至 " + targetFile);

                    try {
                        HandleFile.copyFile(sourceFileName, targetFile);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
